class TireManageName {
    constructor(farsi_name = null, name = null) {
        this.farsi_name = farsi_name;
        this.name = name;
    }
}
class TireSeller extends TireManageName {
    constructor(farsi_name = null, name = null, tire_quantity = null) {
        super(farsi_name, name);
        this.tire_quantity = tire_quantity;
    }
}
class TireBrand extends TireManageName {
    constructor(farsi_name = null, name = null, tireSellers = []) {
        super(farsi_name, name);
        this.tireSellers = {};
        tireSellers.forEach(tireSeller => this.tireSellers[tireSeller.name] = tireSeller);
    }
    setTireSeller(tireSeller) {
        this.tireSellers[tireSeller.name] = tireSeller;
    }
}
class TireSize {
    constructor(size, tireBrands = []) {
        this.tireBrand = {};
        this.size = size;
        tireBrands.forEach(brand => this.tireBrand[brand.name] = brand);
    }
}
class SortedTires {
    constructor(sabok = [], sangin = []) {
        this.sabok = {};
        this.sangin = {};
        sabok.forEach(k => this.sabok[k.size] = k);
        sangin.forEach(k => this.sangin[k.size] = k);
    }
}
class TireInfo {
    constructor(incomingTireInfo) {
        this.sorted_tires = new SortedTires();
        this.elements = { tire_type: null,
            tire_quantity: null,
            tire_size: null,
            tire_brand: null,
            tire_brand_address: null,
            tire_seller: null };
        this.incomingTireInfo = incomingTireInfo;
        incomingTireInfo.data.forEach((data) => {
            const carClass = Number(data.tire_type_is_sabok) === 1 ? 'sabok' : 'sangin';
            const seller = new TireSeller(this.incomingTireInfo.sellers_list_all[data.tire_seller].farsi_name, data.tire_seller, data.tire_quantity);
            const brand = new TireBrand(this.incomingTireInfo.brands_list_all[data.tire_brand].farsi_name, data.tire_brand, [seller]);
            if (typeof (this.sorted_tires[carClass][data.tire_size]) === 'undefined') {
                this.sorted_tires[carClass][data.tire_size] = new TireSize(data.tire_size, [brand]);
            }
            if (typeof (this.sorted_tires[carClass][data.tire_size]['tireBrand'][data.tire_brand]) === 'undefined') {
                this.sorted_tires[carClass][data.tire_size]['tireBrand'][data.tire_brand] = brand;
            }
            if ((typeof this.sorted_tires[carClass][data.tire_size]['tireBrand'][data.tire_brand]['tireSellers'][data.tire_seller]) === 'undefined') {
                this.sorted_tires[carClass][data.tire_size]['tireBrand'][data.tire_brand]['tireSellers'][data.tire_seller] = seller;
            }
        });
        jQuery(document).on('gform_post_render', (event, form_id, current_page) => this.gFormPostRender(event, form_id, current_page, this));
    }
    getTireType() {
        const inputs = jQuery('#gform_' + this.incomingTireInfo.form_id).serializeArray();
        return inputs.find(input => input.name === 'input_' + this.incomingTireInfo.tire_type_id).value;
    }
    getTireSize() {
        const inputs = jQuery('#gform_' + this.incomingTireInfo.form_id).serializeArray();
        return inputs.find(input => input.name === 'input_' + this.incomingTireInfo.size_id).value;
    }
    getTireBrand() {
        const inputs = jQuery('#gform_' + this.incomingTireInfo.form_id).serializeArray();
        return inputs.find(input => input.name === 'input_' + this.incomingTireInfo.brand_id).value;
    }
    getTireQuantity() {
        const inputs = jQuery('#gform_' + this.incomingTireInfo.form_id).serializeArray();
        return Number(inputs.find(input => input.name === 'input_' + this.incomingTireInfo.quantity_id).value);
    }
    getElementId(filed_id, input = true) {
        const elType = input ? 'input' : 'field';
        return jQuery(`#${elType}_${this.incomingTireInfo.form_id}_${filed_id}`);
    }
    setTireSize(tireinfo = this, event = null) {
        const tireSize = this.getTireSize();
        tireinfo.elements.tire_size.input.empty();
        tireinfo.elements.tire_brand.input.empty();
        tireinfo.elements.tire_seller.input.empty();
        const car_type = tireinfo.getTireType();
        const is_sabok = tireinfo.getTireType() === 'sabok' ? 1 : 0;
        const tire_quantity = this.getTireQuantity();
        const sizes_for_quantity = this.incomingTireInfo.data
            .filter(d => (Number(d.tire_quantity) >= Number(tire_quantity) && Number(d.tire_type_is_sabok) === is_sabok))
            .map(d => d.tire_size);
        ;
        const size_options = Object.keys(tireinfo.sorted_tires[car_type])
            .filter(size => sizes_for_quantity.indexOf(size) >= 0)
            .map(size => {
            const option = new Option(size);
            option.selected = size === tireSize;
            return option;
        });
        tireinfo.elements.tire_size.input.append(size_options);
        tireinfo.setTireBrand();
    }
    setSellersAddress(brand, sellers, tireInfo = this) {
        const address = `<h6>${brand}</h6> ${sellers
            .map(seller => tireInfo.incomingTireInfo.sellers_list_all[seller].address.htmlcontent)
            .join('<hr>')}`;
        tireInfo.elements.tire_brand_address.field[0].innerHTML = address;
    }
    setTireBrand(tireinfo = this, event = null) {
        tireinfo.elements.tire_brand.input.empty();
        tireinfo.elements.tire_seller.input.empty();
        const car_type = tireinfo.getTireType();
        const tire_size = tireinfo.getTireSize();
        const brand_options = Object.keys(tireinfo.sorted_tires[car_type][tire_size].tireBrand).map(brand => {
            return new Option(tireinfo.sorted_tires[car_type][tire_size]['tireBrand'][brand].farsi_name, tireinfo.sorted_tires[car_type][tire_size]['tireBrand'][brand].name);
        });
        tireinfo.elements.tire_brand.input.append(brand_options);
        tireinfo.setTireSeller();
    }
    setTireSeller(tireinfo = this, event = null) {
        tireinfo.elements.tire_seller.input.empty();
        const car_type = tireinfo.getTireType();
        const tireQuantity = tireinfo.getTireQuantity();
        const tire_size = tireinfo.getTireSize();
        const tire_brand = tireinfo.getTireBrand();
        const brand = tireinfo.sorted_tires[car_type][tire_size]['tireBrand'][tire_brand];
        const sellers = Object.keys(brand['tireSellers'])
            .filter(seller => Number(brand['tireSellers'][seller].tire_quantity) >= tireQuantity);
        tireinfo.setSellersAddress(brand.farsi_name, sellers);
        const seller_options = sellers
            .map(seller => {
            return new Option(brand['tireSellers'][seller].farsi_name, brand['tireSellers'][seller].name);
        });
        console.log(seller_options);
        tireinfo.elements.tire_seller.input.append(seller_options);
    }
    setAllElements() {
        this.elements.tire_type = { input: null, field: null };
        this.elements.tire_quantity = { input: null, field: null };
        this.elements.tire_size = { input: null, field: null };
        this.elements.tire_brand = { input: null, field: null };
        this.elements.tire_brand_address = { input: null, field: null };
        this.elements.tire_seller = { input: null, field: null };
        this.elements.tire_type.input = this.getElementId(this.incomingTireInfo.tire_type_id);
        this.elements.tire_quantity.input = this.getElementId(this.incomingTireInfo.quantity_id);
        this.elements.tire_size.input = this.getElementId(this.incomingTireInfo.size_id);
        this.elements.tire_brand.input = this.getElementId(this.incomingTireInfo.brand_id);
        this.elements.tire_brand_address.input = this.getElementId(this.incomingTireInfo.seller_address);
        this.elements.tire_seller.input = this.getElementId(this.incomingTireInfo.seller_id);
        this.elements.tire_type.field = this.getElementId(this.incomingTireInfo.tire_type_id, false);
        this.elements.tire_quantity.field = this.getElementId(this.incomingTireInfo.quantity_id, false);
        this.elements.tire_size.field = this.getElementId(this.incomingTireInfo.size_id, false);
        this.elements.tire_brand.field = this.getElementId(this.incomingTireInfo.brand_id, false);
        this.elements.tire_brand_address.field = this.getElementId(this.incomingTireInfo.seller_address, false);
        this.elements.tire_seller.field = this.getElementId(this.incomingTireInfo.seller_id, false);
    }
    gFormPostRender(event, form_id, current_page, tireInfo = this) {
        tireInfo.setAllElements();
        tireInfo.initial();
    }
    initial() {
        this.setAllElements();
        this.setTireSize();
        this.setTireBrand();
        this.setTireSeller();
        this.elements.tire_type.input.on("change", (event) => this.setTireSize(this, event));
        this.elements.tire_quantity.input.on("change", (event) => this.setTireSize(this, event));
        this.elements.tire_size.input.on("change", (event) => this.setTireBrand(this, event));
        this.elements.tire_brand.input.on("change", (event) => this.setTireSeller(this, event));
    }
}
